from curl_cffi import requests
proxies={
    'http': '127.0.0.1:7890',
    'https': '127.0.0.1:7890',
}
import asyncio
import json
import time

from curl_cffi import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import execjs
import hashlib

with open("h5st.js", "r", encoding="utf-8") as f:
    js = f.read()

js_com = execjs.compile(js)


def sha_256(message):
    return hashlib.sha256(message.encode("utf-8")).hexdigest()


def generate_h5st(params):
    # 解析原始 body
    body_str = params["body"]
    body = json.loads(body_str) if isinstance(body_str, str) else body_str

    # 创建用于 SHA256 加密的 boddy
    boddy = {
        "page": body["page"],
        "pagesize": body["pagesize"],
        "area": body["area"],
        "source": body["source"],
        "gb": body["gb"],
    }

    # 创建用于生成 h5st 的 a 对象
    a = {
        "appid": params.get("appid", "www-jd-com"),
        "body": sha_256(json.dumps(boddy, separators=(",", ":"))),
        "clientVersion": params.get("clientVersion", "1.0.0"),
        "client": params.get("client", "pc"),
        "functionId": params.get("functionId", "pc_home_feed"),
        "t": int(time.time() * 1000),  # 使用当前时间戳
    }

    # 调用 JS 的 PSign.sign 方法生成 h5st
    h5st = js_com.call("getH5st", a)

    return h5st




headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "referer": "https://www.jd.com/",
    "sec-ch-ua": '"Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "script",
    "sec-fetch-mode": "no-cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0",
}
cookies = {
    "b_webp": "1",
    "b_avif": "1",
    "jcap_dvzw_fp": "_HIVLiZCIEZDIcHwXjPU43al5uvsYIwPfs4O6VE9nqABptbi3ASG0seWjzU_o2F4bpTtKrH1VphdN9RwziykSA==",
    "whwswswws": "",
    "__jdu": "1751545604909534518369",
    "pinId": "briEibRb-h1YgxkG81uaz7V9-x-f3wj7",
    "pin": "jd_7eac037a858fc",
    "unick": "jd_8js7q5tn2s7n2h",
    "_tp": "aO5XtogPiplRTZPyN6P5Vkd5JC%2FQJe7kEmh0g0oTaNI%3D",
    "_pst": "jd_7eac037a858fc",
    "shshshfpa": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "shshshfpx": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "autoOpenApp_downCloseDate__jd_czlogin_openapp_": "1752224419174_1",
    "b_dw": "1912",
    "b_dh": "954",
    "b_dpr": "1",
    "commonAddress": "0",
    "regionAddress": "1%2C72%2C55674%2C0",
    "autoOpenApp_downCloseDate_autoOpenApp_autoPromptly": "1752227819923_1",
    "autoOpenApp_downCloseDate_jd_homePage": "1752462226750_1",
    "autoOpenApp_downCloseDate_auto": "1752462295553_1800000",
    "pt_st": "1_QNKa_p1pyRBuRGl7XqjS4ZLHq52uvHozXQu0yZZJaZBykE4huAxadU8rhTxkOed5v-IuGcuXOYsk2aIeXaDCbyBsLjrt7MdvOSvBodXxczB1FkZgSNPx2J-jeoajHnfW39S6QiYrPFEIK1rDtYq4I4SRP4BxLa81ywRy9N0XGlJp_KtCaUMwl_dWLrRUp5syXYAGm0trSW0tTUQiv77qSqdfTbhMefstFJyB3qwZ",
    "__jdv": "76161171|direct|-|none|-|1756881433725",
    "TrackID": "1xM3ZQ21DHtuXgiq36XtFIOSXu1mAWZ4x-D5xaVdH7fy-vfDHlmTkQwxI_jvHRLi8LJioNT25c_6PKire-mo6MVOioLX3wONC6NgLWZauD51IyQW3ZNZ5YAfd9rbm-Khp",
    "light_key": "AASBKE7rOxgWQziEhC_QY6ya0t-vlaOmou2BzyMrCts2vOi5coHY-N24rrJkrJnFBW04tveI",
    "3AB9D23F7A4B3C9B": "GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBE",
    "areaId": "19",
    "ipLoc-djd": "19-1607-4773-62123",
    "__jda": "76161171.1751545604909534518369.1751545604.1756881434.1757399499.20",
    "__jdc": "76161171",
    "3AB9D23F7A4B3CSS": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMZFUWENHYAAAAADRQZNB36MH4YDAX",
    "_gia_d": "1",
    "thor": "2B3FAB022DDA7FFF772855629D82E8BA86B6B654FC776E1930AA7FEB1EB3100E1EF5EB759739600F22FCCF1958FE9679B0EEA60481F98D5CEF896FE1C118BC6F5902AEA9B671F28B659099323024C59799B63A1868AC8D1579B564EB1C6725F65D3E137CF8D9B3055385727960FA2DB0582E23CEF871A191CC2BC9FB3D0381D0FCB9E5F8F8D6B912AD4BC29B1A5CA075D1A2E963DC5DF26B14C5B056504FE0AC",
    "mail_times": "4%2C1%2C1757399500426",
    "cn": "12",
    "flash": "3_6RZt43iU0i2knvVUi8JW7q76TptU2XxPAjCq7VJtz2wkKMkKirR8ufugC8xrxXHu_8vEIs8sYTgUEU7J62QUOGJLs7q42WWzYYOEVTMmb6COxNE7S8Cywr4xzrp9TImVmTO0WroSivZ9cxwwsJY0sBFZHQSXPjjUmLKBWEvQWfIsKi6cPpydtq**",
    "PCSYCityID": "CN_440000_440300_0",
    "shshshfpb": "BApXSq_0kLvxAYEqgcJFHsyDPBX7ULrZhBhMVUQdk9xJ1MppMe462",
    "__jdb": "76161171.2.1751545604909534518369|20.1757399499",
    "sdtoken": "AAbEsBpEIOVjqTAKCQtvQu17zkULC-BWsKm1deparZPi7iYqCsjjXLMjaydT6jJZSkijlhP8kY1cwQqVR5K1Q0672dnoy_xMgPwQeaOvaS6d8QA6oSfFojqVvie_",
}
url = "https://api.m.jd.com/"
params = {
    "h5st": "20250909144631545;ttgi9wm6m6wd06d3;b5216;tk05wb73805ac41lMSsyUVNvODgyfZxV9IeV9E6U0IOGq8OH6QaEhhRhIrPTK2nHYYLPu47V68OL6orV88OUqVOUtZOV;26f33e0a2adc6d580f81c34679b9b631;5.2;1757400389545;gt6f-VeIrheIqIOGrtrEo9rItQ6ZB5_ZxI7ZBh-f1ZOVB5_ZzUrJ-hfZXx-Z-h-T-h6Q1E7J8E6ZBh-f1ZPIw9uUq9uU9cbU-I7IrFuUwR7IwBOU9U7U7Q_UxROIsZfZnZvFAI6GAU7ZBh-f1ZfVzZ_WsJqK8wLH7kMU5YfZnZ-E-hfZXx-ZLAKGvRqN38rE-h-T-trG9oLJvYfZB5hW-ZuVz8rM-h-T-JbF-hfZXxPCBh-fvh-T-dOVsY7ZBhfZB5hWtdeZnZfVwN6J-hfZBh-f1BOWB5_ZvdOE-YfZBhfZXx-ZpceDvh_FIY_HSsvK7YfZnZPGyQ7GAY6ZBhfZB5hWxh-T-BOE-YfZBhfZXxfVB5_ZqN6J-hfZBh-f1B_VB5_ZrN6J-hfZBh-f1heZnZPUsY7ZBhfZB5hWxh-T-ROE-YfZBhfZXxPUth-T-VOE-YfZBhfZXx-ZtpPVzh_ZB5_ZwN6J-hfZBh-f1heZnZvHqYfZBhfZXxPUB5_Zuw7ZBhfZB5hWxh-T-x7ZBhfZB5hWxh-T-RrE-hfZBh-fmg-T-R7G8QaD8YfZB5hWkgfZXZvJ7AOUpdrUuhuJAMrUCQ7H-h-T-ZeF-hfZBh-fmg-T-haF-hfZXx-ZtJeDB1eUrpLHKgvTxpfVwhfMTgvFqkbIz8rM-h-T-dLEuYfZB5xD;67d6974d7136d6472c1f49a9c1511edd;gRaW989Gy8bE_oLE7w-Gy8rFvM7MtoLI4wrJ1R6G88bG_wPD9k7J1RLHxgKJ",
    "appid": "www-jd-com",
    "body": '{"page":1,"pagesize":25,"area":"19_1607_4773_62123","source":"pc-home","gb":1}',
    "clientVersion": "1.0.0",
    "client": "pc",
    "functionId": "pc_home_feed",
    "t": "1757399514456",
    "uuid": "76161171.1751545604909534518369.1751545604.1756881434.1757399499.20",
    "loginType": "3",
    "x-api-eid-token": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMZFUWENHYAAAAADRQZNB36MH4YDAX",
    "callback": "jsonpMore2Goods",
    "_": "1757399514463",
}
h5st = generate_h5st(params)
print(h5st)
print(len(h5st))
params["h5st"] = h5st
response = requests.get(url, headers=headers, cookies=cookies, params=params,proxies=proxies)

print(response.text)
print(response)
